package com.yc.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableRabbit
public class RabbitMqConfig {

    // 定义队列名
    public static final String QUEUE_NAME = "promotionQueue";
    // 定义交换机名
    public static final String EXCHANGE_NAME = "promotionExchange";
    // 定义路由键
    public static final String ROUTING_KEY = "promotionRoutingKey";

    // 创建队列
    @Bean
    public Queue queue() {
        return new Queue(QUEUE_NAME, true); // durable为true表示队列持久化
    }

    // 创建交换机
    @Bean
    public TopicExchange exchange() {
        return new TopicExchange(EXCHANGE_NAME, true, false); // durable为true表示交换机持久化，autoDelete为false表示交换机在最后一个绑定解除后不会自动删除
    }

    // 创建绑定
    @Bean
    public Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }

    // 这里可以添加其他RabbitMQ相关的配置，例如消息监听器、消息转换器等
}
